﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Windows.Forms;
using VisaPoint.Entities;
using VisapointLibrary.Controllers;

namespace VisaPoint
{
    public partial class FrmImport : Form
    {
        public FrmImport()
        {
            InitializeComponent();
        }
        public void sendMail()
        {
            //MailMessage mail = new MailMessage();
            //SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            //mail.From = new MailAddress("tuyennv.haui@gmail.com");
            //mail.To.Add("tuyennv.haui@gmail.com");
            //mail.Subject = "Test Mail";
            //mail.Body = "This is for testing SMTP mail from GMAIL";

            //SmtpServer.Port = 587;
            //SmtpServer.Credentials = new System.Net.NetworkCredential("tuyennv.haui@gmail.com", "33649326");
            //SmtpServer.EnableSsl = true;

            //SmtpServer.Send(mail);
            SmtpClient client = new SmtpClient();
            client.Port = 45;
            client.Host = "smtp.googlemail.com";
            client.EnableSsl = true;
            client.Timeout = 10000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential("tuyennv.haui@gmail.com", "33649326");

            MailMessage mm = new MailMessage("tuyennv.haui@gmail.com", "tuyennv.haui@gmail.com", "test", "test");
            mm.BodyEncoding = UTF8Encoding.UTF8;
            mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            client.Send(mm);
            MessageBox.Show("mail Send");

        }
        private void sendmail2()
        {
            var client = new SmtpClient("smtp.googlemail.com", 25)
            {
                Credentials = new NetworkCredential("tuyennv.haui@gmail.com", "33649326"),
                EnableSsl = true
            };
            client.Send("tuyennv.haui@gmail.com", "tuyennv.haui@gmail.com", "test", "testbody");
            Console.WriteLine("Sent");
            Console.ReadLine();
        }
        private void btnRegister_Click(object sender, EventArgs e)
        {
            //sendmail2();
            Appointment obj = new Appointment();
            obj.Citizenship = cbCitizenship.Text;
            obj.Purpose = cbPurpose.Text;
            obj.FirstName = txtName.Text;
            obj.FamilyName = txtFamily.Text;
            obj.DateOfBirth = txtDateOfBirth.Text;
            obj.Gender = cbGender.Text;
            obj.PassportNumber = txtPassportNumber.Text;
            obj.Email = txtEmail.Text;
            obj.PhoneNumber = txtPhoneNumber.Text;
            string strAppointment = JsonConvert.SerializeObject(obj);
            File.WriteAllText(Application.StartupPath + "\\Appointment.txt", strAppointment);
            FrmVisapoint frmVisapoint = new FrmVisapoint(obj);
            this.Hide();
            frmVisapoint.ShowDialog();
            this.Show();
        }
        private System.Drawing.Image cropImage(System.Drawing.Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            return bmpImage.Clone(cropArea, bmpImage.PixelFormat);
        }
        private void FrmImport_Load(object sender, EventArgs e)
        {
            //string PathFile = Application.StartupPath + "\\captcha.jpg";
            //System.Drawing.Image image = System.Drawing.Image.FromFile(PathFile);
            //System.Drawing.Image imgCrop = cropImage(image, new Rectangle(new Point(0, 0), new Size(250, 60)));
            //imgCrop.Save(Application.StartupPath + "\\captcha_Cropt.jpg");
            string strAppointment = "";
            try
            {

                strAppointment = File.ReadAllText(Application.StartupPath + "\\Appointment.txt");
                if (!string.IsNullOrEmpty(strAppointment))
                {
                    Appointment obj = JsonConvert.DeserializeObject<Appointment>(strAppointment);
                    cbCitizenship.Text = obj.Citizenship;
                    cbPurpose.Text = obj.Purpose;
                    txtName.Text = obj.FirstName;
                    txtFamily.Text = obj.FamilyName;
                    txtDateOfBirth.Text = obj.DateOfBirth;
                    cbGender.Text = obj.Gender;
                    txtPassportNumber.Text = obj.PassportNumber;
                    txtEmail.Text = obj.Email;
                    txtPhoneNumber.Text = obj.PhoneNumber;
                }
            }
            catch (Exception)
            {

            }

        }
    }
}
